package cheeese.and.crackers;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

/**
 * New Task Activity Class
 * @author Cheese and Crackers
 *
 */
public class NewTaskActivity extends Activity{
	
	private UserList users;
	private User activeUser;
	
	private Button cancelButton;
	private Button okButton;
	private DatePicker dueDateDatePicker;
	private Spinner categorySpinner;
	
	/** 
	 * Called when the activity is first created. 
     * 
     * @param savedInstanceSate Saved state of activity when last suspended
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.newtaskscreen);
        users = new UserList();
        
        try {
			users.load(getApplicationContext());
		} catch (Exception e) {
			Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
    		try {
    			Thread.sleep(2000);
    		} 
    		catch(InterruptedException e2) {} 
    		android.os.Process.killProcess(android.os.Process.myPid());
		}
		//Get Active User
 		Bundle activeUserBundle = this.getIntent().getExtras();
 		String activeUserString = activeUserBundle.getString("ACTIVE_USER");
 		activeUser = users.getUser(activeUserString);
 		
 		cancelButton = (Button) findViewById(R.id.buttonCancel);
 		okButton = (Button) findViewById(R.id.buttonOK);
 		dueDateDatePicker = (DatePicker) findViewById(R.id.datePickerDueDate);
 		categorySpinner = (Spinner) findViewById(R.id.spinnerCategory);
 		
 		String[] categories = {"Personal", "School", "Work"};
 		
 		ArrayAdapter<String> categoryAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
 		
 		categorySpinner.setAdapter(categoryAdapter);
 		
 		NewTaskClickListener myListener = new NewTaskClickListener();
 		
 		cancelButton.setOnClickListener(myListener);
 		okButton.setOnClickListener(myListener);
    }
    
    /**
     * Method to call when activity is paused
     */
     @Override
     public void onPause(){
    	super.onPause();
     	try{
     		users.save(getApplicationContext());
     	}
     	catch(Exception e){
     		Toast.makeText(getApplicationContext(), (CharSequence)("Fatal Error With User Storage - Shutting Down"), Toast.LENGTH_SHORT).show();
     		try {
     			Thread.sleep(2000);
     		} 
     		catch(InterruptedException e2) {} 
     		android.os.Process.killProcess(android.os.Process.myPid());
     	}
     }
     
     /**
      * Listener for button
      */
 	public class NewTaskClickListener implements OnClickListener {
     	@Override
     	/**
     	 * Checks which button was pressed and takes appropriate action, login or create user
     	 * @param v The view which was clicked
     	 */
 		public void onClick(View v) {
     			if(v==okButton){
     				String displayDescription = ((EditText)findViewById(R.id.textDisplayDescription)).getText().toString();
     				String location = ((EditText)findViewById(R.id.textLocation)).getText().toString();
     				String details = ((EditText)findViewById(R.id.textDetails)).getText().toString();
     				//Month plus one? unknown error?
     				int month = dueDateDatePicker.getMonth() + 1;
     				String stringMonth = Integer.toString(month);
     				if(month < 10){
     					stringMonth = "0"+stringMonth;
     				}
     				String dueDate = (stringMonth + "/" + dueDateDatePicker.getDayOfMonth() + "/" + dueDateDatePicker.getYear());
     				
     				String type =  (String) categorySpinner.getSelectedItem();
     				
     				Task newTask = new Task(displayDescription, location, dueDate, details, type, false);
     				
     				try{
     					activeUser.getTasks().addTask(newTask);
     					Toast.makeText(getApplicationContext(), (CharSequence)("Task Add Successful"), Toast.LENGTH_SHORT).show();
     					finish();
     				}
     				catch(Exception e){
     					Toast.makeText(getApplicationContext(), (CharSequence)(e.getMessage()), Toast.LENGTH_SHORT).show();
     				}
     			}	
     			else if(v==cancelButton){
     				finish();
     			}
 		}
 	}
}
